package com.cpi.cornpp.costelement.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.form.CostElementListForm;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cs.common.utils.StringUtils;


public class CostElementAction extends MappingDispatchAction {
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAIL_FORWARD = "fail";//$NON-NLS-1$
	/** Cost Element List Request Atribute  */
	private static final String O_COSTELEMENT_LIST = "elementList";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Identifier for the cost element*/
	private static final String CE_ID = "ceId";
	/** List forward**/
	private static final String LIST = "list";	
	/** Identifier in the Request for info messages*/
	private static final String ERROR_EMPTY_DESCRIPTION = "error.ceu.emptydescription";
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	/**
	 * Action to get a list of all Cost Elements
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
		CostElementServices costElementServices = null;
		Collection collectionCostElementView = null;
		HttpSession httpSession = null;
		CostElementListForm costElementListForm = null;
		int orderField = 1;
		int orderType = 0;
		try{
			costElementListForm = (CostElementListForm)form;
			httpSession = request.getSession(false);
			costElementServices = new CostElementServices();
			collectionCostElementView = new ArrayList(0);
			if( costElementListForm != null ){
				if( costElementListForm.getOrderField() > 0 ){
					orderField = costElementListForm.getOrderField();
				}
				if( costElementListForm.getOrderType() > 0 ){
					orderType = costElementListForm.getOrderType();
				}
			}
			collectionCostElementView = costElementServices.getCostElementList(orderField, (1 == orderType) ? false : true);
			request.setAttribute(O_COSTELEMENT_LIST, collectionCostElementView);
		}catch (CostElementException costElementException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		} finally {
			httpSession.removeAttribute("deletedCostElementTransport");
			httpSession.removeAttribute("addedCostElementTransport");
			httpSession.removeAttribute("costElementView");
			httpSession.removeAttribute("transportsList");
		}
		httpSession.setAttribute("previousOrderField", Integer.toString(orderField));
		httpSession.setAttribute("previousOrderType", Integer.toString(orderType));
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward goAddCostElement(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CostElementView costElementView = null;
		HttpSession session = null;
		ActionForward forward = null;
		
		session = request.getSession(false);
		forward = mapping.findForward(FAIL_FORWARD);
		
		costElementView = new CostElementView();
		
		session.setAttribute("costElementView", costElementView);
		
		forward = mapping.findForward(SUCCESS_FORWARD);
		
		return forward;
	}
	
	/**
	 * Action to Insert a Cost Element
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws CostElementException {
		CostElementServices costElementService = null;
		CostElementForm costElementForm =null;
		String forward = null;
		String messageError = null;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			costElementForm =  (CostElementForm) form;
			costElementService= new CostElementServices();
			if (costElementForm !=null) {
				costElementService.insertCostElement(costElementForm.getCostElementView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
				forward = SUCCESS_FORWARD;
			}
		}catch (CostElementException costeElementException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), costeElementException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, costeElementException);
				form.reset(mapping, request);
				throw costeElementException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			throw costElementException;
		}
		form.reset(mapping, request);
		return mapping.findForward(forward);
	}

	/**
	 * Action to delete a Cost Element
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws CostElementException {
		
		CostElementServices costElementService = null;
		int costElementId = 0;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		String messageError = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			costElementId = Integer.parseInt(request.getParameter(CE_ID));
			costElementService= new CostElementServices();
			costElementService.deleteCostElement(costElementId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (CostElementException costeElementException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, costeElementException);
			throw costeElementException;
		}catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		}			
		return mapping.findForward(DEFAULT_FORWARD);
	}	
	
	public ActionForward goUpdate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws CostElementException {
		String ceId = null;
		int costElementId = 0;
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		CostElementServices costElementService = null;
		
		try{
			costElementForm = (CostElementForm)form;
			ceId = request.getParameter(CE_ID);
			if(ceId==null){
				return mapping.findForward(LIST);
			}else{
				costElementId = Integer.parseInt(ceId);
			}
			costElementService = new CostElementServices();
			costElementView = costElementService.getCostElementById(costElementId);
			costElementForm.setCostElementView(costElementView);
		}catch (CostElementException costElementException){
			throw costElementException;
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			throw costElementException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Action to update a cost element
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		CostElementServices costElementService = null;
		CostElementForm costElementForm =null;
		MessageResources messageResources = null;
		String message = null;
		HttpSession httpSession = null;
		costElementForm =  (CostElementForm) form;		
		String messageError = null;
		String forward = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			if(StringUtils.emptyString(costElementForm.getCostElementView().getCostElementDescription())){
				message = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), ERROR_EMPTY_DESCRIPTION);
				httpSession.setAttribute(ERROR_MESSAGE, message);
				return mapping.findForward(FAIL_FORWARD);
			}
			costElementService= new CostElementServices();
			if (costElementForm !=null) {			
				costElementService.updateCostElement(costElementForm.getCostElementView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
				forward = SUCCESS_FORWARD;
			}
		}
		catch (CostElementException costeElementException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), costeElementException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, costeElementException);
				form.reset(mapping, request);
				throw costeElementException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
		}
		catch (Exception e){
			CostElementException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CostElementException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			form.reset(mapping, request);
			throw costElementException;
		}
		form.reset(mapping, request);
		return mapping.findForward(forward);
	}	
}
